<?php
class TrackingController extends Core_Controller_Action {
    
    public function indexAction() {
        $filters = array();
        $filters['ref_no'] = $this->_getParam("ref_no");
        $filters['ship_name'] = $this->_getParam("ship_name");
        $filters['port_of_call'] = $this->_getParam("port_of_call");
        $filters['ata'] = $this->_getTimestampParam('ata');
        //Zend_Debug::dump(date(DATE_FORMAT, $this->_getTimestampParam('ata')));die();
        $filters['ats'] = $this->_getTimestampParam('ats');
        $this->view->filters = $filters;
        
        $transaction_db = new Core_Model_DbTable_Transactions();
        $paginator_adapter = $transaction_db->getPaginatorAdapter($filters);
        if(is_null($paginator_adapter)){
            $this->_handlePageNotFound();
        }
        $paginator = new Zend_Paginator($paginator_adapter);

        $page = $this->_getParam("page", 0);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage(SHIP_PER_PAGE);
        
        $ships = $paginator->getCurrentItems();
        $general_info_ids = array();
        foreach ($ships as &$ship) {
            $general_info_ids[] = $ship['general_information_id'];
        }
        $general_info_db = new Core_Model_DbTable_GeneralInformation();
        $general_infos = $general_info_db->getMulti($general_info_ids);
        
        $port_ids = array();
        foreach ($ships as &$ship) {
            $general_info_id = $ship['general_information_id'];
            $general_info = $general_infos[$general_info_id];
            $ship['general_info'] = $general_info;
            $port_ids[] = $general_info['port_id'];
        }
        
        $port_db = new Core_Model_DbTable_Ports();
        $ports = $port_db->getMulti($port_ids);

        foreach ($ships as &$ship) {
            $port_id = $ship['general_info']['port_id'];
            if ( isset($ports[$port_id]) ) {
                $port = $ports[$port_id];
            } else {
                $port = NULL;
            }
            $ship['port'] = $port;
        }
        
        $this->view->ships = $ships;
        $this->view->paginator = $paginator;
        $this->view->ports = $port_db->getAll();
    }
}
?>
